package com.zy.controller;

import com.alibaba.fastjson.JSON;
import com.zy.bean.Product;
import com.zy.bean.ProductType;
import com.zy.service.IProductTypeService;
import com.zy.service.impl.ProductTypeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author GongXl
 * @Description 商品管理
 * @Date 012 11:58
 * @Param
 * @return
**/
@WebServlet("/mall")
public class MallController extends DispactherServlet {
    private IProductTypeService iProductTypeService = new ProductTypeService();
    /**
     * @Author GongXl
     * @Description 获得菜单的数据
     * @Date 017 14:58
     * @Param [req, res]
     * @return java.lang.String
     *
     * {
     *   "msg": "success",
     *   "stats": 200,
     *   "productTypeList": [
     *     {
     *       "name": "家用电器"
     *     },
     *     {
     *       "name": "手机/运营商/数码"
     *     },
     *     {
     *       "name": "电脑办公"
     *     }
     *   ]
     * }
    **/
    public void getMenuTypeListJson(HttpServletRequest req, HttpServletResponse res) throws IOException {
        res.setContentType("application/json;charset=utf-8");
        List<ProductType> productTypeList = iProductTypeService.getProductTypeList();
        Map productTypeMap  = new HashMap();
        productTypeMap.put("msg","success");
        productTypeMap.put("status",200);
        productTypeMap.put("productTypeList",productTypeList);
        String productTypeJson = JSON.toJSONString(productTypeMap);
        res.getWriter().write(productTypeJson);
        res.getWriter().flush();
        res.getWriter().close();
    }

}
